﻿//======================================================================
//  对文件和文件夹的操作类
//====================================================================== 

using System.IO;

namespace Common
{
    using System;
    using System.Data;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;

    /// <summary>
    /// 对文件和文件夹的操作类
    /// </summary>
    public class FilePlus
    {
        #region 判断文件是否是图片

        /// <summary>
        /// 判断文件是否是图片
        /// </summary>
        /// <param name="fileName">文件名称，包含扩展名</param>
        /// <returns></returns>
        public static bool isPicFile(string fileName)
        {
            bool ispic = false;
            if (fileName != string.Empty && fileName != null)
            {
                if (fileName.IndexOf(".") > -1)
                {
                    int dotPosition = fileName.IndexOf(".");
                    string getFile = fileName.Substring(dotPosition).ToLower();
                    switch (getFile.ToLower())
                    {
                        case ".jpg":
                        case ".gif":
                        case ".jpeg":
                        case ".png":
                        case ".bmp":
                        case ".ico":
                        case ".jpe":
                        case ".tif":
                        case ".tiff":
                            ispic = true;
                            break;
                        default:
                            break;
                    }
                }
            }
            return ispic;
        }

        #endregion

        #region 判断文件是否是压缩文件

        /// <summary>
        /// 判断文件是否是压缩文件
        /// </summary>
        /// <param name="fileName">文件名称，包含扩展名</param>
        /// <returns></returns>
        public static bool IsZipFile(string fileName)
        {
            bool isZip = false;
            if (!string.IsNullOrEmpty(fileName))
            {
                if (fileName.IndexOf(".") > -1)
                {
                    int dotPosition = fileName.IndexOf(".");
                    string getFile = fileName.Substring(dotPosition).ToLower();
                    switch (getFile.ToLower())
                    {
                        case ".zip":
                        case ".rar":
                        case ".jar":
                        case ".cab":
                        case ".iso":
                        case ".tar":
                        case ".uue":
                            isZip = true;
                            break;
                        default:
                            break;
                    }
                }
            }
            return isZip;
        }

        #endregion

        #region 判断文件是否是影音媒体文件

        /// <summary>
        /// 判断文件是否是影音媒体文件
        /// </summary>
        /// <param name="fileName">文件名称，包含扩展名</param>
        /// <returns></returns>
        public static bool IsMediaFile(string fileName)
        {
            bool isMedia = false;
            if (!string.IsNullOrEmpty(fileName))
            {
                if (fileName.IndexOf(".") > -1)
                {
                    int dotPosition = fileName.IndexOf(".");
                    string getFile = fileName.Substring(dotPosition).ToLower();
                    switch (getFile.ToLower())
                    {
                        case ".wav":
                        case ".mp1":
                        case ".mp2":
                        case ".mp3":
                        case ".mp4":
                        case ".mp5":
                        case ".ra":
                        case ".rm":
                        case ".ram":
                        case ".wma":
                        case ".mid":
                        case ".avi":
                        case ".mpeg":
                        case ".mpg":
                        case ".dat":
                        case ".asf":
                        case ".mov":
                        case ".real":
                            isMedia = true;
                            break;
                        default:
                            break;
                    }
                }
            }
            return isMedia;
        }

        #endregion

        #region 判断文件是否是常用文本文件

        /// <summary>
        /// 判断文件是否是常用文本文件txt,doc,xls
        /// </summary>
        /// <param name="fileName">文件名称，包含扩展名</param>
        /// <returns></returns>
        public static bool IsTxtFile(string fileName)
        {
            bool isTxt = false;
            if (!string.IsNullOrEmpty(fileName))
            {
                if (fileName.IndexOf(".") > -1)
                {
                    int dotPosition = fileName.IndexOf(".");
                    string getFile = fileName.Substring(dotPosition).ToLower();
                    switch (getFile.ToLower())
                    {
                        case ".txt":
                        case ".doc":
                        case ".xls":
                            isTxt = true;
                            break;
                        default:
                            break;
                    }
                }
            }
            return isTxt;
        }

        #endregion

        #region 获取指定目录下的文件路径
        /// <summary>
        /// 获取指定目录下的文件路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static ArrayList FileList(string path)
        {
            ArrayList arrayList = new ArrayList();
            string[] strFile = Directory.GetDirectories(path);
            if (strFile.Length == 0)
            {
                return new ArrayList();
            }
            foreach (string file in strFile)
            {
                arrayList.Add(file);
            }
            return arrayList;
        } 
        #endregion
    }
}
